/*
 * BALog.java
 * classes : com.qim.basdk.utilites.BALog
 * @author xuwei
 * V 1.0.0
 * Created at 2015年8月26日 下午5:38:40
 */
package com.langsheng.lsintell.utils;

import android.os.Environment;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * com.qim.basdk.utilites.LSLog
 * <p>
 * 自定义log,打印log文件
 *
 * @author xuwei
 *         <p>
 *         created at 2015年8月26日 下午5:38:40
 */
public class LSLog {
    public final static String TAG = "LSLog";

    // log打印开关
    public final static boolean LOG_DEBUG = true;

    // log文件记录开关
    public final static boolean LOG_WRITE = false;

    /**
     * @param tag
     * @param info
     */
    public static void v(String tag, String info) {
        if (LOG_DEBUG) {
            Log.v(tag, info);
        }
        writeLogToFile(tag, info, null);
    }

    /**
     * @param tag
     * @param info
     * @param throwable
     */
    public static void v(String tag, String info, Throwable throwable) {
        if (LOG_DEBUG) {
            Log.v(tag, info, throwable);
        }
        writeLogToFile(tag, info, throwable);
    }

    /**
     * @param tag
     * @param info
     */
    public static void i(String tag, String info) {
        if (LOG_DEBUG) {
            Log.i(tag, info);
        }
        writeLogToFile(tag, info, null);
    }

    /**
     * @param tag
     * @param info
     * @param throwable
     */
    public static void i(String tag, String info, Throwable throwable) {
        if (LOG_DEBUG) {
            Log.i(tag, info, throwable);
        }
        writeLogToFile(tag, info, throwable);
    }

    /**
     * @param tag
     * @param info
     */
    public static void d(String tag, String info) {
        if (LOG_DEBUG) {
            Log.d(tag, info);
        }
        writeLogToFile(tag, info, null);
    }

    /**
     * @param tag
     * @param info
     * @param throwable
     */
    public static void d(String tag, String info, Throwable throwable) {
        if (LOG_DEBUG) {
            Log.d(tag, info, throwable);
        }
        writeLogToFile(tag, info, throwable);
    }

    /**
     * @param tag
     * @param info
     */
    public static void e(String tag, String info) {
        if (LOG_DEBUG) {
            Log.e(tag, info);
        }
        writeLogToFile(tag, info, null);
    }

    /**
     * @param tag
     * @param info
     * @param throwable
     */
    public static void e(String tag, String info, Throwable throwable) {
        if (LOG_DEBUG) {
            Log.e(tag, info, throwable);
        }
        writeLogToFile(tag, info, throwable);
    }

    /**
     * @param TAG
     * @param log
     * @param throwable
     */
    private static void writeLogToFile(String TAG, String log, Throwable throwable) {
        if (!LOG_WRITE) {
            return;
        }
        try {
            String fileName = Environment.getExternalStorageDirectory() + "/imLog.txt";
            File file = new File(fileName);
            String stacktrace = "";

            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault());
            Date date = new Date();
            String time = formatter.format(date);
            if (file.exists() && file.length() == 1024 * 1024) {
                file.delete();
            }

            if (!file.exists()) {
                file.createNewFile();

                String model = android.os.Build.MODEL;
                String device = android.os.Build.DEVICE;
                String version = android.os.Build.VERSION.RELEASE;
                String fingerprint = android.os.Build.FINGERPRINT;

                stacktrace = "time: " + time + "\n";
                stacktrace += "model:" + model + "\n";
                stacktrace += "device:" + device + "\n";
                stacktrace += "version:" + version + "\n";
                stacktrace += "fingerprint:" + fingerprint + "\n\n";
            }

            stacktrace += time + ":\n" + TAG + ":" + log + "\n";

            if (throwable != null) {
                final Writer result = new StringWriter();
                final PrintWriter printWriter = new PrintWriter(result);
                throwable.printStackTrace(printWriter);
                stacktrace += result.toString() + "\n";
            }

            FileOutputStream fos = new FileOutputStream(file, true);
            OutputStreamWriter out = new OutputStreamWriter(fos);
            BufferedWriter bw = new BufferedWriter(out);
            bw.write(stacktrace);
            bw.newLine();
            bw.close();
            fos.flush();
            out.close();
            fos.close();
        } catch (Exception e) {

        }
    }

}
